<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="68px">
      <el-form-item label="材料类型" prop="materialType">
        <el-input
          v-model="queryParams.materialType"
          placeholder="请输入材料类型"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['LegalCase:case_identifications_materials:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="case_identifications_materialsList">
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="案件id" align="center" prop="caseId" />
      <el-table-column label="材料类型" align="center" prop="materialType" />
      <el-table-column label="材料描述" align="center" prop="materialDescription" />
      <el-table-column label="文件路径" align="center">
        <template #default="scope">
          <el-link type="primary" target="_blank" :href="`http://127.0.0.1:8890${scope.row.filePath}`">
            {{ `http://127.0.0.1:8890${scope.row.filePath}` }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['LegalCase:case_identifications_materials:edit']"
          >修改
          </el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['LegalCase:case_identifications_materials:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
      @change="(currentPage, pageSize) => {this.queryParams.pageNum = currentPage;this.getList()}"
    />

    <!-- 添加或修改案件鉴定材料对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="案件id" prop="caseId">
          <el-select v-model="form.caseId" placeholder="请选择案件id">
            <el-option
              v-for="item in caseRegistrationList"
              :key="item.id"
              :label="`${item.consulationId}-${item.createTime}`"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="材料类型" prop="materialType">
          <el-input v-model="form.materialType" placeholder="请输入材料类型" />
        </el-form-item>
        <el-form-item label="材料描述" prop="materialDescription">
          <el-input v-model="form.materialDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文件路径" prop="filePath">
          <file-upload v-model="form.filePath" @input="uploadChange" :value="form.filePath" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCase_identifications_materials,
  getCase_identifications_materials,
  delCase_identifications_materials,
  addCase_identifications_materials,
  updateCase_identifications_materials
} from '@/api/LegalCase/case_identifications_materials'
import { listCase_registration } from '@/api/LegalCase/case_registration.js'

export default {
  name: 'Case_identifications_materials',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 案件鉴定材料表格数据
      case_identifications_materialsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialType: null,
        materialDescription: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        caseId: [
          { required: true, message: '案件id不能为空', trigger: 'change' }
        ],
        materialType: [
          { required: true, message: '材料类型不能为空', trigger: 'blur' }
        ],
        materialDescription: [
          { required: true, message: '材料描述不能为空', trigger: 'blur' }
        ]
      },
      caseRegistrationList: []
    }
  },
  created() {
    this.getList()
    this.getCaseRegistrationList()
  },
  methods: {
    getCaseRegistrationList() {
      listCase_registration().then(resp => {
        this.caseRegistrationList = resp.rows
      })
    },
    /** 查询案件鉴定材料列表 */
    getList() {
      if (this.$route.query.id) {
        this.queryParams.caseId = this.$route.query.id
      }
      this.loading = true
      listCase_identifications_materials(this.queryParams).then(response => {
        this.case_identifications_materialsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        caseId: null,
        materialType: null,
        materialDescription: null,
        filePath: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加案件鉴定材料'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCase_identifications_materials(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改案件鉴定材料'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCase_identifications_materials(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCase_identifications_materials(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除案件鉴定材料编号为"' + ids + '"的数据项？').then(function() {
        return delCase_identifications_materials(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
    uploadChange(e) {
      this.form.filePath = e
    }
  }
}
</script>
